package io.wins.vertx.service.impl;

import io.vertx.core.Vertx;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.wins.vertx.entity.Carousel;
import io.wins.vertx.service.BaseService;
import io.wins.vertx.service.CarouselService;

public class CarouselServiceImpl extends BaseService implements CarouselService {
    public CarouselServiceImpl(final Vertx vertx) {
        this.vertx = vertx;
    }

    @Override
    public CarouselService getCarouselById(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_carousel where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public CarouselService listCarousel(final Message tMessage) {
        final StringBuilder sb = new StringBuilder();
        sb.append("select * from ds_carousel where is_delete = '0'");
        final JsonObject param = ((JsonObject) tMessage.body()).getJsonObject("param");
        if (param != null && "1".equals(param.getString("isActive"))) {
            sb.append(" and is_active = '1' order by seq asc");
        }
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, sb.toString());
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public CarouselService createCarousel(final Carousel carousel, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "insert into ds_carousel (id, title, image_id, url, seq, is_active, created_on, updated_on) VALUES (?, ?, ?, ?, ?, ?, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())")
            .put(PARAMS, new JsonArray()
                .add(carousel.getId())
                .add(carousel.getTitle())
                .add(carousel.getImageId())
                .add(carousel.getUrl())
                .add(carousel.getSeq())
                .add(carousel.isActive()));
        this.insertSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public CarouselService updateCarousel(final Carousel carousel, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_carousel set title = ?, image_id = ?, url = ?, seq = ?, is_active = ?, updated_on = UNIX_TIMESTAMP() where id = ?")
            .put(PARAMS, new JsonArray()
                .add(carousel.getTitle())
                .add(carousel.getImageId())
                .add(carousel.getUrl())
                .add(carousel.getSeq())
                .add(carousel.isActive())
                .add(carousel.getId()));
        this.insertSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public CarouselService deleteCarousel(final String id, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_carousel set is_delete = '1' where id = ?")
            .put(PARAMS, new JsonArray().add(id));
        this.deleteSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }
}
